package com.bizmerce.rnd.oms.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * HttpServletRequest Data 
 * @author kcs
 */
public class ParamMap extends HashMap <String, Object> {
    
    private static final long serialVersionUID = -6278535657417034793L;
    
    public String getString(String key) {
        return getString(key, "");
    }
    
    public String getString(String key, String def) {
        
        String s = String.valueOf(super.get(key));

        if (s == null || s.equals("") || s == "null")
            return def;
        else
            return s;
    }

    public int getInt(String key) {
        return getInt(key, 0);
    }

    public int getInt(String key, int def) {
        String s = String.valueOf(super.get(key));

        if (s == null || s.equals("") || s == "null")
            return def;
        else
            return Integer.parseInt(s);
    }

    public double getDouble(String key) {
        return getDouble(key, 0d);
    }

    public double getDouble(String key, double def) {

        String s = String.valueOf(super.get(key));

        if (s == null || s.equals("") || s == "null")
            return def;
        else
            return Double.parseDouble(s);
    }


    public long getLong(String key) {
        return getLong(key, 0L);
    }

    public long getLong(String key, long def) {

        String s = String.valueOf(super.get(key));

        if (s == null || s.equals("") || s == "null")
            return def;
        else
            return Long.parseLong(s);
    }

    public float getFloat(String key) {
        return getFloat(key, 0f);
    }

    public float getFloat(String key, float def) {

        String s = String.valueOf(super.get(key));

        if (s == null || s.equals("") || s == "null")
            return def;
        else
            return Float.parseFloat(s);
    }

    public byte [] getByte(String key) {
        return (byte[])super.get(key);
    }
    
    @SuppressWarnings("unchecked")
    public List <String> getList(String key) {
        List <String> list = null;

        Object ob = super.get(key);

        if (ob instanceof String) {
            list = new ArrayList <String>();
            list.add(String.valueOf(ob));
        } else if (ob instanceof String []) {
            list = new ArrayList<String>();
            String [] s = (String []) ob;

            for (int i=0 ; i < s.length ; i++)
                list.add(s[i]);

        } else if (ob instanceof List) {
            list = (List<String>)ob;
        } else {
            list = new ArrayList<String>();
        }

        return list;
    }

    public String [] getArray(String key) {
        return this.getArray(key, "");
    }

    @SuppressWarnings("unchecked")
    public String [] getArray(String key, String def) {
        
        Object ob = super.get(key);
        
        String [] arr = null;

        if (ob instanceof String) {
            arr = new String[] {(String)ob};
            
        } else if (ob instanceof String []) {
            arr = (String []) ob;

        } else if (ob instanceof List) {
            List <String> list = (List<String>) ob;
            arr = new String[list.size()];
            System.arraycopy(list.toArray(), 0, arr, 0, list.size());

        } else {
            arr = new String[] {};
        }
       
        for (int i = 0; i < arr.length; i++)
            if (arr[i] == null || arr[i].equals("") || "null".equals(arr[i]))
                arr[i] = def;
        
        return arr;
    }
}